import java.util.ArrayList;
import java.util.List;

/**
 * 784. 字母大小写全排列
 */
public class No784 {
    List<String> answer = new ArrayList<>();
    char[] path;

    public List<String> letterCasePermutation(String s) {
        char[] charArray = s.toCharArray();
        path = new char[charArray.length];
        dfs(charArray, 0);
        return answer.stream().distinct().toList();
    }

    public void dfs(char[] charArray, int index) {
        if (index == charArray.length) {
            answer.add(new String(path));
            return;
        }
        for (int i = 0; i < 2; i++) {
            char c = charArray[index];
            if (i % 2 == 0) {
                if (c >= 'A' && c <= 'Z') {
                    c += 32;
                } else if (c >= 'a' && c <= 'z') {
                    c -= 32;
                } else {
                    continue;
                }
            }
            path[index] = c;
            dfs(charArray, index + 1);
        }
    }
}
